function httpRequest(url, callback) {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", url, true);
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
      callback(xhr.responseText);
    }
  };
  xhr.send();
}

function showWeather(result) {
  console.log(result);
  result = JSON.parse(result);
  var list = result.list;
  var table = '<table><tr><th>日期</th><th>天气</th><th>最低温度</th><th>最高温度</th></tr>';
  for (var i in list) {
    var data = list[i];
    var d = new Date(data.dt * 1000);
    table += '<tr>';
    table += '<td>' + d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ":00" + '</td>';
    table += '<td>' + data.weather[0].description + '</td>';
    table += '<td>' + Math.round(data.main.temp_min - 273.15) + ' °C</td>';
    table += '<td>' + Math.round(data.main.temp_max - 273.15) + ' °C</td>';
    table += '</tr>';
  }
  table += '</table>';
  document.getElementById('weather').innerHTML = table;
}

var city = localStorage.city;
city = city ? city : 'beijing';
var url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&APPID=cd2bfe940bb257a9d3c94310a45cf9f3&lang=zh_cn";
// var url = 'http://api.openweathermap.org/data/2.5/forecast/daily?q=' + city + ',china&lang=zh_cn';
url = "http://api.openweathermap.org/data/2.5/forecast?q=" + city + "&appid=cd2bfe940bb257a9d3c94310a45cf9f3&lang=zh_cn";
httpRequest(url, showWeather);